<?php
class Link 
{
	private $link = '';
	private $name = '';
	private $id = '';
	
	function Link() 
	{
		
	}
	
	function setData($data) 
	{
		$this->id = $data['id'];
		$this->name = $data['name'];
		$this->link = $data['link'];
	}
	
	function getID() 
	{
		return $this->name;
	}
	
	function getName() 
	{
		return $this->name;
	}
	
	function getBody()
	{
		return $this->link;
	}
	
	function getLink($linkid)
	{
		$query ="SELECT id,name,link FROM links WHERE id = $linkid";

		$row = getAssoc($query);
			
		$pos = new Link();
		$pos->setData($row);

		return $pos;
	}
	
	function getAll() 
	{
		$links = array();
		
		//select comments, for this post id
		$query = "SELECT id,name,link FROM links ORDER BY name ASC";
		
		//rezultati iskanja
		$result = getFullAssoc($query);
		
		foreach ($result as $re)
		{
			$temp =new Link();
			$temp->setData($re);
			$links[] = $temp;
		}
		
		return $links;
	}
	
	function displayLinkInList()
	{
		echo '
		<div class="comments-template">
			<ol class="commentlist">
	
				<li>
			
					<h3><a href="'.WEBSITE_URL.'index.php?action=admin&amp;link=edit&amp;linkid='.$this->id.'" title="Edit '.$this->name.'">'.$this->name.'</a> @ '.$this->link.' </h3>

					<a href="'.WEBSITE_URL.'index.php?action=admin&amp;link=edit&amp;linkid='.$this->id.'" rel="bookmark" title="Edit '.$this->name.'">(edit)</a> | <a href="?action=admin&amp;link=delete&amp;linkid='.$this->id.'" rel="bookmark" title="Delete '.$this->name.'">(delete)</a>				
				</li>
				
			</ol>
		</div>	
		
	';
	}
	
	function addLink() 
	{
		$name =  $_REQUEST['name'];
		$link =  $_REQUEST['url'];
		
		//create sql query
		$sql = "INSERT INTO `links` (`name`, link) VALUES ('$name', '$link')";
	
		$result = query($sql);
		
		echo 'Link added succesfully!';
	}
	
	function updateLink() 
	{
		$id =  $_REQUEST['linkid'];
		$name =  $_REQUEST['name'];
		$link =  $_REQUEST['url'];
		
		//create sql query
		$sql = "UPDATE links SET name = '$name',link = '$link' WHERE id = $id LIMIT 1";

		$result = query($sql);
		
		echo 'Link updated succesfully!';
	}
	
	function deleteLink() 
	{		
		$id =  $_REQUEST['linkid'];

		//create sql query
		$sql = "DELETE FROM links WHERE id = $id LIMIT 1";
	
		$result = query($sql);
		
		echo 'Link deleted succesfully!';
	}
}

?>